#include <rclcpp/rclcpp.hpp>
using namespace std::chrono_literals;
class ParamClientNode: public rclcpp::Node
{

private:
    rclcpp::SyncParametersClient::SharedPtr param_client;    
public:
    using SharedPtr = std::shared_ptr<ParamClientNode>;
    ParamClientNode():Node("param_clientn_node")
    {
        RCLCPP_INFO(this->get_logger(),"ParamClientNode创建");
        /*
        1,参数服务端启动后，底层封装了多个服务通信的服务端
        */
        this->param_client=std::make_shared<rclcpp::SyncParametersClient>(this,
            "param_server_node"
        );
    }
public:
    bool connect_param_server()
    {
        while(!this->param_client->wait_for_service(1s))
        {
            if(!rclcpp::ok())
            {
                return false;
            }
            RCLCPP_INFO(this->get_logger(),"服务连接中");
        }
        RCLCPP_INFO(this->get_logger(), "成功连接到参数服务端！");
        return true;
    }
    void get_param()
    {
        RCLCPP_INFO(this->get_logger(),"----------------查询操作-----------------");
        std::string car_name=this->param_client->get_parameter<std::string>("car_name");
        double width=this->param_client->get_parameter<double>("width");
        RCLCPP_INFO(this->get_logger(),"width=%.2f",width);
        RCLCPP_INFO(this->get_logger(),"car_name=%s",car_name.c_str());
        std::vector<rclcpp::Parameter> params = this->param_client->get_parameters(
            {"car_name", "wheels", "width"});
        for(rclcpp::Parameter param:params)
        {
            RCLCPP_INFO(this->get_logger(),"s %s = %s",param.get_name().c_str(),
            param.value_to_string().c_str());
        }


    }   
    void add_param()
    {
        RCLCPP_INFO(this->get_logger(),"----------------增加操作-----------------");
    }
    void update_param()
    {
        RCLCPP_INFO(this->get_logger(),"----------------修改操作-----------------");
        this->param_client->set_parameters({
            rclcpp::Parameter("car_name","sanling"),
            rclcpp::Parameter("width",3.0),
            rclcpp::Parameter("length",8.0)
        });
        std::vector<rclcpp::Parameter> params = this->param_client->get_parameters(
            {"car_name", "wheels", "width"});
        for(rclcpp::Parameter param:params)
        {
            RCLCPP_INFO(this->get_logger(),"s %s = %s",param.get_name().c_str(),
            param.value_to_string().c_str());
        }
    }
    void delete_param()
    {
        RCLCPP_INFO(this->get_logger(),"----------------删除操作-----------------");
    }
};
int main(int argc, char ** argv)
{
    
    rclcpp::init(argc,argv);
    //std::shared_ptr<ParamClientNode> client_node = std::make_shared<ParamClientNode>();
    ParamClientNode::SharedPtr client_node = std::make_shared<ParamClientNode>();
    if(!client_node->connect_param_server())
    {
        return 1;
    }
    client_node->get_param();
    client_node->update_param();
    client_node->get_param();
    // client_node->add_param();
    // client_node->delete_param();
    rclcpp::spin(client_node);
    rclcpp::shutdown();
    return 0;
}